From f2b7fd73a93a88c2864f2c068f76316f83bfd850 Mon Sep 17 00:00:00 2001 From: Andrew Watts Date: Mon, 17 Apr 2017 21:54:56 +0930 Subject: [PATCH] Fix #2997: error message when version not found could be improved This changes the error message when a package *is* found but there's no matching version to be a little more helpful. Old: "no matching package named `...`" New: "no matching version `...` found for package `...`" --- src/cargo/core/resolver/mod.rs | 67 +++++++++++++++++++++------------- tests/build.rs | 3 +- tests/registry.rs | 9 ++--- 3 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/cargo/core/resolver/mod.rs b/src/cargo/core/resolver/mod.rs index 40bb734db..896b28046 100644 --- a/src/cargo/core/resolver/mod.rs +++ b/src/cargo/core/resolver/mod.rs @@ -643,14 +643,6 @@ fn activation_error(cx: &Context, // Note that we re-query the registry with a new dependency that // allows any version so we can give some nicer error reporting // which indicates a few versions that were actually found. - let msg = format!("no matching package named `{}` found \ - (required by `{}`)\n\ - location searched: {}\n\ - version required: {}", - dep.name(), parent.name(), - dep.source_id(), - dep.version_req()); - let mut msg = msg; let all_req = semver::VersionReq::parse("*").unwrap(); let new_dep = dep.clone_inner().set_version_req(all_req).into_dependency(); let mut candidates = match registry.query(&new_dep) { @@ -660,27 +652,50 @@ fn activation_error(cx: &Context, candidates.sort_by(|a, b| { b.version().cmp(a.version()) }); - if !candidates.is_empty() { - msg.push_str("\nversions found: "); - for (i, c) in candidates.iter().take(3).enumerate() { - if i != 0 { msg.push_str(", "); } - msg.push_str(&c.version().to_string()); - } - if candidates.len() > 3 { - msg.push_str(", ..."); + + let msg = if !candidates.is_empty() { + let versions = { + let mut versions = candidates.iter().take(3).map(|cand| { + cand.version().to_string() + }).collect::>(); + + if candidates.len() > 3 { + versions.push("...".into()); + } + + versions.join(", ") + }; + + let mut msg = format!("no matching version `{}` found for package `{}` \ + (required by `{}`)\n\ + location searched: {}\n\ + versions found: {}", + dep.version_req(), + dep.name(), + parent.name(), + dep.source_id(), + versions); + + // If we have a path dependency with a locked version, then this may + // indicate that we updated a sub-package and forgot to run `cargo + // update`. In this case try to print a helpful error! + if dep.source_id().is_path() + && dep.version_req().to_string().starts_with("=") { + msg.push_str("\nconsider running `cargo update` to update \ + a path dependency's locked version"); } - } - // If we have a path dependency with a locked version, then this may - // indicate that we updated a sub-package and forgot to run `cargo - // update`. In this case try to print a helpful error! - if dep.source_id().is_path() && - dep.version_req().to_string().starts_with("=") && - !candidates.is_empty() { - msg.push_str("\nconsider running `cargo update` to update \ - a path dependency's locked version"); + msg + } else { + format!("no matching package named `{}` found \ + (required by `{}`)\n\ + location searched: {}\n\ + version required: {}", + dep.name(), parent.name(), + dep.source_id(), + dep.version_req()) + }; - } human(msg) } diff --git a/tests/build.rs b/tests/build.rs index 1eea86b5c..eccccbe6c 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -760,9 +760,8 @@ fn compile_path_dep_then_change_version() { assert_that(p.cargo("build"), execs().with_status(101).with_stderr("\ -[ERROR] no matching package named `bar` found (required by `foo`) +[ERROR] no matching version `= 0.0.1` found for package `bar` (required by `foo`) location searched: [..] -version required: = 0.0.1 versions found: 0.0.2 consider running `cargo update` to update a path dependency's locked version ")); diff --git a/tests/registry.rs b/tests/registry.rs index d99415264..b336827ab 100644 --- a/tests/registry.rs +++ b/tests/registry.rs @@ -134,9 +134,8 @@ fn wrong_version() { assert_that(p.cargo("build"), execs().with_status(101).with_stderr_contains("\ -[ERROR] no matching package named `foo` found (required by `foo`) +[ERROR] no matching version `>= 1.0.0` found for package `foo` (required by `foo`) location searched: registry [..] -version required: >= 1.0.0 versions found: 0.0.2, 0.0.1 ")); @@ -145,9 +144,8 @@ versions found: 0.0.2, 0.0.1 assert_that(p.cargo("build"), execs().with_status(101).with_stderr_contains("\ -[ERROR] no matching package named `foo` found (required by `foo`) +[ERROR] no matching version `>= 1.0.0` found for package `foo` (required by `foo`) location searched: registry [..] -version required: >= 1.0.0 versions found: 0.0.4, 0.0.3, 0.0.2, ... ")); } @@ -399,9 +397,8 @@ fn relying_on_a_yank_is_bad() { assert_that(p.cargo("build"), execs().with_status(101).with_stderr_contains("\ -[ERROR] no matching package named `baz` found (required by `bar`) +[ERROR] no matching version `= 0.0.2` found for package `baz` (required by `bar`) location searched: registry [..] -version required: = 0.0.2 versions found: 0.0.1 ")); } -- 2.30.2